package com.sz.app;


import com.sz.app.edi.rest.edi.order.dto.CusOrderDto;
import com.sz.app.edi.service.order.CusOrderService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

public class CxfClient {
//    public static void main(String[] args) {
//        //cl2();
//        beanToXML();
//    }

    /**
     * 方式1.代理类工厂的方式,需要拿到对方的接口
     */
    @Test
    public void cl1() {
        try {
            // 接口地址
            String address = "http://localhost:9999/services/CusOrderService?wsdl";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(CusOrderService.class);
            // 创建一个代理接口实现
            CusOrderService cs = (CusOrderService) jaxWsProxyFactoryBean.create();
            String data = "<root><token>8792a8570ac84a03a65390d4a3ced55c10ca26da995a4684978742ce04b4d0ad</token><funCode>00001</funCode><data><userName>tom</userName><age>199</age></data></root>";
            // 调用代理接口的方法调用并返回结果
            String result = cs.saveOrder(data);

            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 动态调用方式
     */
    @Test
    public void cl2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:9999/services/CusOrderService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
        // PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("saveOrder", "<root></root>");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

    public String beanToXML() {
        CusOrderDto cusOrderDto = getOrdOrderDto();
        try {
            JAXBContext context = JAXBContext.newInstance(CusOrderDto.class);
            Marshaller marshaller = context.createMarshaller();
            final StringWriter stringWriter = new StringWriter();
            marshaller.marshal(cusOrderDto, stringWriter);

            String chartXml = stringWriter.toString();
            System.out.println(chartXml);
            return chartXml;


        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Test
    public void XMLStringToBean() {
        String xmlStr = beanToXML();
        try {
            JAXBContext context = JAXBContext.newInstance(CusOrderDto.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            CusOrderDto student = (CusOrderDto) unmarshaller.unmarshal(new StringReader(xmlStr));
            System.out.println(student);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }


    private CusOrderDto getOrdOrderDto() {
        CusOrderDto dto = new CusOrderDto();
       /* dto.setCustomerId(1119);
        dto.setCusUserId(1150);
        dto.setCargoType("GT001");
        dto.setProductUid("315202512087093248");
        dto.setFromName("王麻子");
        dto.setType(2);
        OrderAddressModel fromAddressModel = new OrderAddressModel();
        fromAddressModel.setCountryCode("CN");
        fromAddressModel.setProvinceId("110000");//北京
        fromAddressModel.setCityId("110100");//北京市
        fromAddressModel.setDistrictId("110114");//怀柔区
        fromAddressModel.setStreetId("110114001");//杨宋镇
        fromAddressModel.setAddress("前进路28号2301室");
        fromAddressModel.setPostcode("34342323");
        fromAddressModel.setId(2);


        dto.setFromAddress(fromAddressModel);
        dto.setFromPhone("13720053852");
        dto.setFromPostcode("34342323");
        dto.setPackageNum(20);
        dto.setCusWeight(new BigDecimal(2));
        dto.setCusWeightUnit("kg");

        OrderAddressModel toAddressModel = new OrderAddressModel();
        toAddressModel.setCountryCode("CN");
        toAddressModel.setProvinceId("610000");//陕西省
        toAddressModel.setCityId("610100");//西安市
        toAddressModel.setDistrictId("610111");//雁塔区
        toAddressModel.setStreetId("610111007");//丈八沟街道
        toAddressModel.setAddress("绿地世纪城8011室");
        toAddressModel.setPostcode("34342323");
        toAddressModel.setId(3);
        dto.setToAddress(toAddressModel);


        dto.setToName("zhangsan");
        dto.setToPhone("13720053852");
        dto.setOrderStatus(OrderStatus.DRAFT);


        //向产品订单中添加商品明细信息
        OrdOrderCargoDto orderCargoDto = new OrdOrderCargoDto();
        orderCargoDto.setGoodsNameCn("苹果");
        orderCargoDto.setGoodsNameEn("apple");
        orderCargoDto.setGoodsNumber(2);
        orderCargoDto.setGoodsUnit("件");
        orderCargoDto.setGoodsPrice(new BigDecimal(22));
        orderCargoDto.setHsCode("2323");
        dto.addCargoDto(orderCargoDto);

        // OrdSubOrderDto subOrderDto = new

        //向产品中添加可选服务

        OrdOrderAdditionalDto orderAdditionalDto = new OrdOrderAdditionalDto();
        orderAdditionalDto.setServiceTypeCode("ST001");
        dto.addOrderAdditionalDto(orderAdditionalDto);*/
        return dto;
    }

}